package com.zgjkhis.modules.his.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zgjkhis.common.service.impl.BaseServiceImpl;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.modules.his.domain.InfoTransmission;
import com.zgjkhis.modules.his.service.InfoTransmissionService;
import com.zgjkhis.modules.his.service.dto.DataPrescription;
import com.zgjkhis.modules.his.service.dto.DataPrescriptionDetail;
import com.zgjkhis.modules.his.service.mapper.InfoTransmissionMapper;
import com.zgjkhis.modules.system.domain.Order;
import com.zgjkhis.modules.system.service.InfoService;
import com.zgjkhis.modules.system.service.OrderService;
import com.zgjkhis.modules.util.CharUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @Author yzq
 * @Description 处方传输
 * @Date 2021/6/23 9:47
 * @Param
 * @return
 **/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "user")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class InfoTransmissionServiceImpl extends BaseServiceImpl<InfoTransmissionMapper, InfoTransmission> implements InfoTransmissionService {

    private final InfoService infoService;

    private final OrderService orderService;

    @Override
    public List<DataPrescription> queryList() {
        List<DataPrescription> list = infoService.queryDataList();
        List<InfoTransmission> infoTransmissions = new ArrayList<>();
        for (DataPrescription dataPrescription : list) {
            InfoTransmission infoTransmission = new InfoTransmission();
            infoTransmission.setInfoNo(dataPrescription.getId());
            List<DataPrescriptionDetail> detailList = infoService.queryDataDetailList(dataPrescription.getId(), dataPrescription.getQuantityDay());
            for (int i = 0; i < detailList.size(); i++) {
                detailList.get(i).setNo(String.valueOf(i + 1));
            }
            dataPrescription.setDetailList(detailList);
            infoTransmissions.add(infoTransmission);
        }
        this.saveBatch(infoTransmissions);
        return list;
    }

    @Override
    public List<DataPrescription> queryInfoByOrderNo(String orderNo) {
        Order order = orderService.getOne(Wrappers.<Order>query().lambda().eq(Order::getOrderNo, orderNo).or().eq(Order::getApiOrderNo, orderNo));
        if (CharUtil.isEmpty(order)) {
            throw new ServiceException("订单不存在");
        }
        List<DataPrescription> list = infoService.queryInfoByOrderNo(order.getOrderNo());
        for (DataPrescription dataPrescription : list) {
            List<DataPrescriptionDetail> detailList = infoService.queryDataDetailList(dataPrescription.getId(), dataPrescription.getQuantityDay());
            for (int i = 0; i < detailList.size(); i++) {
                detailList.get(i).setNo(String.valueOf(i + 1));
            }
            dataPrescription.setDetailList(detailList);
        }
        return list;
    }
}
